package com.asia.action.ods;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.asia.service.ods.ODSService;
import com.asia.domain.ods.OdsFieldTable;
import com.asia.service.ods.CsvDataIOService;
import com.asia.domain.ods.OdsTable;
import com.util.HibernateUtil;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.opensymphony.xwork2.ActionSupport;

public class CsvDataIO extends ActionSupport
{
    private List<OdsTable> list1=null;
    private List<OdsFieldTable> list2=null;
    private CsvDataIOService csvDataIOService;
    private ODSService odsService;

    public String LoadCSV() throws IOException
    {
        OdsFieldTable odsFieldTable = new OdsFieldTable();

        int num = 0;
        int testnum=0;
        String tableSpecification = null;
        String temptableSpecification = null;
        String tableName = null;
        String tableInterface=null;
        String fieldName = null;
        File[] files = new File("D:\\test").listFiles();//到目录下取文件

        BufferedReader br = null;
        try {
            for (File file : files)
            {//遍历所有文件
                if (!file.isFile())
                {//读取一行
                    continue;
                }
                br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));//获得文件流
                String line = null;
                while ((line = br.readLine()) != null)
                {//读取一行
                    String[] parts = line.split(",");
                    if (1==parts.length)
                    {
                        tableSpecification = parts[0];
                        continue;
                    }
                    //获取每行','分隔出的内容，并存入相应的位置
                    tableName = parts[0].toUpperCase();
                    odsFieldTable.setOdsTableName(tableName);
                    odsFieldTable.setOdsFieldMainFileColumnNumber(parts[1].toUpperCase());
                    fieldName = parts[2].toUpperCase();
                    odsFieldTable.setOdsFieldName(fieldName);
                    odsFieldTable.setOdsFieldDatatype(parts[3].toUpperCase());
                    odsFieldTable.setOdsFieldSpecification(parts[4].toUpperCase());
                    odsFieldTable.setOdsFieldDimen(parts[5].toUpperCase());
                    odsFieldTable.setOdsFieldMoreinfo(parts[6].toUpperCase().length() > 1000 ? parts[6].toUpperCase().substring(0,1000):parts[6].toUpperCase());
                    odsFieldTable.setOdsFieldColumnNumber(parts[7].toUpperCase());
                    tableInterface = parts[8].toUpperCase();
                    odsFieldTable.setOdsInterfaceId(tableInterface);

                    for (num=0;num<parts.length;num++)
                    {//记录每步解析出的值，需替换为logback
                        System.out.println("第"+num+"步:"+parts[num].toUpperCase());
                    }
                    if (temptableSpecification!=tableSpecification)
                    {
                        list1 = odsService.AccurateSearchODS(tableInterface);
                        temptableSpecification=tableSpecification;
                        if (list1.size()>0)
                        {
                            System.out.println("已存在目标ODS表只需插入字段信息！");
                        }
                        else {
                            OdsTable odsTable = new OdsTable();
                            odsTable.setOdsInterfaceId(tableInterface);
                            odsTable.setOdsSpecification(tableSpecification);
                            odsTable.setOdsTableName(tableName);
                            csvDataIOService.SaveOdsTable(odsTable);

                            list1 = null;
                            System.out.println("已添加新ODS表");
                        }
                    }
                    if (odsService.isODSFieldExist(tableName, fieldName, tableInterface))
                    {
                        System.out.println("已存在相同字段");
                        System.out.println(testnum++);
                    }
                    else {
                        if(csvDataIOService.SaveOdsFieldTable(odsFieldTable))
                        {
                            System.out.println("添加字段成功");
                        }
                        else {
                            System.out.println("添加字段失败");
                            return "false";
                        }
                    }
                    System.out.println(testnum++);
                }
            }
            return SUCCESS;
        }
        catch (Exception e) {
            // TODO: handle exception
            System.out.println("出错");
            return "false";
        }
        finally {
            if (br != null)
            {
                br.close();
            }
        }
    }

    public CsvDataIOService getCsvDataIOService() {
        return csvDataIOService;
    }

    public ODSService getOdsService() {
        return odsService;
    }

    public void setOdsService(ODSService odsService) {
        this.odsService = odsService;
    }

    public void setCsvDataIOService(CsvDataIOService csvDataIOService) {
        this.csvDataIOService = csvDataIOService;
    }
}